import Sockets from '../services/socket';

export default {
  namespace: 'room',
  state: {
    data: null,
    msg: {
      userObj: null,
    },
    poker: null,
    common: [],
  },
  reducers: {
    init(state, { payload: data }) {
      return { ...state, data };
    },
    msg(state, { payload: msg }) {
      return { ...state, msg };
    },
    beton(state, { payload: stake }) {
      return { ...state, stake };
    },
    deal(state, { payload: poker }) {
      return { ...state, poker };
    },
    draw(state, { payload: draws }) {
      const common = state.common.concat(draws);
      return { ...state, common };
    },
    // discard(state,{payload:data}){
    //   return {...state,data}
    // }
  },
  effects: {
    plan({ payload: { roomNum } }) {
      Sockets.emit('plan', { roomNum });
    },
    start({ payload: { roomNum } }) {
      const id = Sockets.id;
      Sockets.emit('start', { roomNum, id });
    },
    bet({ payload: { roomNum, price, userName } }) {
      Sockets.emit('bet', { roomNum, price, userName });
    },
    call({ payload: { roomNum, userName } }) {
      Sockets.emit('call', { roomNum, userName });
    },
    discard({ payload: { roomNum, userName } }) {
      const id = Sockets.id;
      Sockets.emit('discard', { roomNum, userName, id });
    },
  },
  subscriptions: {
    getDate({ dispatch }) {
      Sockets.on('connect', () => {
        const id = Sockets.id;
        Sockets.on(id, (data) => {
          dispatch({ type: 'deal', payload: data });
        });
        Sockets.on('beton', (data) => {
          dispatch({ type: 'beton', payload: data });
        });
        Sockets.on('wager', (data) => {
          dispatch({ type: 'msg', payload: data });
        });
        Sockets.on('draw', (data) => {
          dispatch({ type: 'draw', payload: data });
        });
      });
    },

  },
};
